/*
 * StandardControls - standard implementations of Control
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
import flash.Lib;

class StandardControls
{
	
	// note for alignment:
	// drawRoundRect adds a little bit around the outside,
	// so all border positions will be overdrawn slightly
	
	public static function textButton(text : String, 
	font : String, size : Int,
	fontcolor : Int,
	backcolor : Int, backalpha : Float,
	hicolor : Int, hialpha : Float,
	linecolor : Int, linealpha : Float,
	left : Float, top : Float, ?forcewidth : Float) : Control
	{

		var newtext = new flash.text.TextField();
		var newformat = new flash.text.TextFormat();

		newformat.font = font;
		newformat.size = size;
		newformat.align = flash.text.TextFormatAlign.CENTER;
		newformat.color = fontcolor;
		newtext.defaultTextFormat = newformat;
		newtext.selectable = false;

		newtext.text = text;
		newtext.width = newtext.textWidth*1.3;
				
		if (forcewidth!=null)
		{
			newtext.width = forcewidth;
		}
		newtext.height = newtext.textHeight*1.15;
		newtext.embedFonts = true;

		var newcontrol = new Control(left,top,left+newtext.width,top+newtext.height);
		var spr = new flash.display.Sprite();
		spr.x = left;
		spr.y = top;
		spr.addChild(newtext);
		newcontrol.sprite = spr;
		Lib.current.addChild(spr);
		
		newcontrol.extra = [newtext,newformat];
		
		var over = function()
		{
			spr.graphics.clear();
			spr.graphics.lineStyle(2,linecolor,linealpha);
			spr.graphics.beginFill(hicolor,hialpha);
			spr.graphics.drawRoundRect(0,0,newcontrol.width,newcontrol.height,16,16);
		}
		var frame = function()
		{
			spr.graphics.clear();
			spr.graphics.lineStyle(2,linecolor,linealpha);
			spr.graphics.beginFill(backcolor,backalpha);
			spr.graphics.drawRoundRect(0,0,newcontrol.width,newcontrol.height,16,16);
		}
		var click = function()
		{
			if (newcontrol.tween!=null)
				newcontrol.tween.curframe = 0;
		}
		newcontrol.onMouseOver = over;
		newcontrol.onFrame = frame;
		newcontrol.onClick = click;
		newcontrol.active = true;

		newcontrol.onFrame();
		
		return newcontrol;
	}

	public static function listTextButton(textlist : Array<String>, 
	font : String, size : Int,
	fontcolor : Int,
	backcolor : Int, backalpha : Float,
	hicolor : Int, hialpha : Float,
	linecolor : Int, linealpha : Float,
	left : Float, top : Float, ysepdist : Float, 
	?forcewidth : Float) : Array<Control>
	{

		var newcontrols : Array<Control> = new Array();

		for (n in textlist)
		{
			newcontrols.push(textButton(n, font, size, fontcolor,
							backcolor, backalpha, hicolor, hialpha,
							linecolor, linealpha, left, top, forcewidth));
			top+=newcontrols[newcontrols.length-1].height+ysepdist;
		}

		return newcontrols;

	}

	public static function textEntryButton(text : String, 
	font : String, size : Int,
	fontcolor : Int,
	backcolor : Int, backalpha : Float,
	hicolor : Int, hialpha : Float,
	linecolor : Int, linealpha : Float,
	center : Float, top : Float, numchars : Int, restrict : String,
	width : Int) : Control
	{
		
		var newcontrol = 
		textButton(text,font,size,fontcolor,backcolor,
		backalpha,hicolor,hialpha,linecolor,linealpha,center-(width/2),
		top,width);

		var txt : flash.text.TextField;
		txt = newcontrol.extra[0];
				
		newcontrol.onClick = function () 
		{
			Lib.current.stage.focus = txt;
			txt.setSelection(txt.text.length,txt.text.length);
		}

		var spr = newcontrol.sprite;
		
		txt.autoSize = flash.text.TextFieldAutoSize.CENTER;		
		txt.maxChars = numchars;
		txt.restrict = restrict;
		txt.type = flash.text.TextFieldType.INPUT;
		txt.selectable = true;
		
		return newcontrol;
	}
}
